package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @program: tensquare_parent
 * @description: 吐槽控制层
 * @author: NieMiao
 * @create: 2018-12-26 16:56
 **/
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    /** 
    * @Description: 查询所有数据
    * @Param: [] 
    * @return: entity.Result 
    * @Author: NieMiao 
    * @Date: 2018/12/28 
    */ 
    @GetMapping
    public Result findAll(){
        return new Result(true,StatusCode.OK,"查询成功",spitService.findAll());
    }
    /**
    * @Description: 根据id查询
    * @Param: [id]
    * @return: entity.Result
    * @Author: NieMiao
    * @Date: 2018/12/28
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",spitService.findById(id));
    }
    /**
    * @Description: 添加一条数据
    * @Param: [spit]
    * @return: entity.Result
    * @Author: NieMiao
    * @Date: 2018/12/28
    */
    @PostMapping()
    public Result add(@RequestBody Spit spit){
        spitService.add(spit);
        return new Result(true,StatusCode.OK,"添加成功");
    }
    /** 
    * @Description: 根据id更新数据 
    * @Param: [id, spit] 
    * @return: entity.Result 
    * @Author: NieMiao 
    * @Date: 2018/12/28 
    */ 
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id,@RequestBody Spit spit){
        spit.set_id(id);
        spitService.update(spit);
        return new Result(true,StatusCode.OK,"更新成功");
    }
    
    /** 
    * @Description: 根据id删除数据
    * @Param: [id] 
    * @return: entity.Result 
    * @Author: NieMiao 
    * @Date: 2018/12/28 
    */ 
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id){
        spitService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
    /** 
    * @Description: 根据父节点id查询分页数据
    * @Param: [parentId, page, size] 
    * @return: entity.Result 
    * @Author: NieMiao 
    * @Date: 2018/12/28 
    */ 
    @GetMapping(value = "/comment/{parentId}/{page}/{size}")
    public Result findByParentId(@PathVariable String parentId,
                                 @PathVariable Integer page,
                                 @PathVariable Integer size){
        Page<Spit> spitPage = spitService.findByParentId(parentId, page, size);
        PageResult<Spit> result = new PageResult<>(spitPage.getTotalElements(),spitPage.getContent());
        return new Result(true,StatusCode.OK,"查询成功",result);
    }
    /** 
    * @Description: 给吐槽点赞 
    * @Param: [id] 
    * @return: entity.Result 
    * @Author: NieMiao 
    * @Date: 2018/12/28 
    */ 
    @PutMapping(value = "/thumbup/{id}")
    public Result updateThumbup(@PathVariable String id){
        spitService.updateThumbup(id);
        return new Result(true,StatusCode.OK,"点赞成功");
    }

}
